home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15463 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  117 lines

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Another class/function pointer problem...
  5. Date: Fri, 05 Apr 1996 11:52:41 -0500
  6. Organization: Datalytics, Inc
  7. Message-ID: <31654FD9.1F6E@datalytics.com>
  8. References: <315FE1D1.E66@cs.utwente.nl>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Angelo Starink wrote:
  16. > Since I tried to use a callback function within an object, I'm
  17. > suffering from the class/function pointer problem as well...
  18. > Using the "static" trick it seemed like the problem was solved
  19. > but now I can't get reach my class member variables:
  20. > class Problem {
  21. > public:
  22. >         static CALLBACK void fn(...,...,..);
  23. >         HWND the_window_using_this_object;
  24. > };
  25. > [snip]
  26. > When I try to compile this, Visual C++ 2.0 says:
  27. > "'Problem::the_window_using_this_object' does not specify an object"
  28.  
  29. Your problem is that the HWND is a dm of class Problem.  There 
  30. is a separate the_window_using_this_object in each Problem 
  31. object.  A static mf has no this pointer and is not associated 
  32. with a particular Problem instance as a result.
  33.  
  34. One way to make this work is to make 
  35. the_window_using_this_object a static dm.  Then, fn can access 
  36. it.  This approach means there can be only one window associated 
  37. with the callback at a time.
  38.  
  39. class Problem
  40. {
  41.    public:
  42.       static CALLBACK void fn(...);
  43.    private:
  44.       static HWND m_hWindow;
  45. };
  46.  
  47. CALLBACK void Problem::fn(...)
  48. {
  49.    ::MessageBox(
  50.       m_hWindow,
  51.       "Whatever...",
  52.       "Don't care",
  53.       MB_OK);
  54. }
  55.  
  56. The only other solution is to save the current object's this 
  57. pointer in a static dm and then access 
  58. the_window_using_this_object through it.  This approach has the 
  59. advantage of having an object associated with each interested 
  60. window, and the object is associated with the callback only 
  61. until the callback is invoked.
  62.  
  63. class Problem
  64. {
  65.    public:
  66.       static CALLBACK void fn(...);
  67.       void                 RegisterCallback(void);
  68.    private:
  69.       HWND  m_hWindow;
  70.  
  71.       static Problem*   s_pThis;
  72. };
  73.  
  74. inline
  75. void Problem::RegisterCallback(void)
  76. {
  77.    ASSERT(!s_pThis);
  78.    s_pThis = this;
  79. }
  80.  
  81. CALLBACK void Problem::fn(...)
  82. {
  83.    Problem* pThis = s_pThis;
  84.    s_pThis = 0;
  85.    ::MessageBox(
  86.       s_pThis->m_hWindow,
  87.       "Whatever...",
  88.       "Don't care",
  89.       MB_OK);
  90. }
  91.  
  92. Both solutions raise the spectre of multithreaded safety.  As 
  93. soon as you introduce static dms into a class, you have to 
  94. protect access to them in a multithreaded application.  You can 
  95. protect access several ways.  One way relegates all access to a 
  96. single thread.  By convention, you simply avoid accesses in any 
  97. other thread.  This is safe, provided it is well documented and 
  98. it is easy to determine the thread from which you are accessing 
  99. these objects.
  100.  
  101. Another means to make the accesses thread safe is to protect 
  102. them with an NT critical section, mutex, or semaphore.  The 
  103. logical thing would be to have RegisterCallback lock access to 
  104. the static dm, and (probably) have fn release the lock.  Then, 
  105. you serialize setting and responding to the data stored in the 
  106. static dm.
  107.  
  108. -- 
  109. Robert Stewart        | My opinions are usually my own.
  110. Datalytics, Inc.    | stew@datalytics.com
  111.